﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class DmPBCTDataAccess
    {
        public void CreateInvoice(string ps_Subsidiary, object pd_Ngay_Ct, object ps_Dien_Giai, object ps_Desc_EN, object ps_Ma_Tte, object ps_UserName, object pi_Posted, object pb_Locked, DataTable poDetail)
        {
            if ((poDetail != null) && (poDetail.Rows.Count != 0))
            {
                string str = AppDataAccess.Cong_So_Tt(ps_Subsidiary);
                int num = 0;
                CTKTDataAccess access = new CTKTDataAccess();
                object obj2 = access.Cong_So_Ct(ps_Subsidiary, "PKCT", DBNull.Value, pd_Ngay_Ct, DBNull.Value);
                decimal num2 = 0M;
                SqlConnection eZConnection = AppDataAccess.AppConnection;
                SqlTransaction transaction = eZConnection.BeginTransaction();
                SqlCommand command = new SqlCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Transaction = transaction;
                SqlParameter[] values = new SqlParameter[0x13];
                foreach (DataRow row in poDetail.Rows)
                {
                    num2 += (decimal) row["Tien"];
                    num++;
                    values[0] = new SqlParameter("p_Stt0", row["Stt0_No"]);
                    values[1] = new SqlParameter("p_Stt", str);
                    values[2] = new SqlParameter("p_Stt_NV", num);
                    values[3] = new SqlParameter("p_No_Co", "N");
                    values[4] = new SqlParameter("p_Tk", row["Tk_No"]);
                    values[5] = new SqlParameter("p_Ma_Tte", ps_Ma_Tte);
                    values[6] = new SqlParameter("p_Ty_Gia", 1);
                    values[7] = new SqlParameter("p_Ty_Gia_Ht", 1);
                    values[8] = new SqlParameter("p_Tien_Nt", row["Tien"]);
                    values[9] = new SqlParameter("p_Tien", row["Tien"]);
                    values[10] = new SqlParameter("p_Ma_Bp", DBNull.Value);
                    values[11] = new SqlParameter("p_Ma_Hd", DBNull.Value);
                    values[12] = new SqlParameter("p_Ma_Dt", DBNull.Value);
                    values[13] = new SqlParameter("p_Han_Tt", DBNull.Value);
                    values[14] = new SqlParameter("p_Ma_Km", DBNull.Value);
                    values[15] = new SqlParameter("p_Ma_Sp", row["Ma_Sp"]);
                    values[0x10] = new SqlParameter("p_Dien_Giai", ps_Dien_Giai);
                    values[0x11] = new SqlParameter("p_Desc_EN", ps_Desc_EN);
                    values[0x12] = new SqlParameter("p_Result", SqlDbType.BigInt);
                    values[0x12].Direction = ParameterDirection.Output;
                    values[0x12].SqlDbType = SqlDbType.Int;
                    try
                    {
                        command.CommandText = "CtK0_Save";
                        command.Parameters.Clear();
                        command.Parameters.AddRange(values);
                        command.ExecuteNonQuery();
                    }
                    catch (Exception exception)
                    {
                        throw new NotSupportedException("Invalid expression", exception);
                    }
                    num++;
                    values[0] = new SqlParameter("p_Stt0", row["Stt0_Co"]);
                    values[1] = new SqlParameter("p_Stt", str);
                    values[2] = new SqlParameter("p_Stt_NV", num);
                    values[3] = new SqlParameter("p_No_Co", "C");
                    values[4] = new SqlParameter("p_Tk", row["Tk_Co"]);
                    values[5] = new SqlParameter("p_Ma_Tte", ps_Ma_Tte);
                    values[6] = new SqlParameter("p_Ty_Gia", 1);
                    values[7] = new SqlParameter("p_Ty_Gia_Ht", 1);
                    values[8] = new SqlParameter("p_Tien_Nt", row["Tien"]);
                    values[9] = new SqlParameter("p_Tien", row["Tien"]);
                    values[10] = new SqlParameter("p_Ma_Bp", DBNull.Value);
                    values[11] = new SqlParameter("p_Ma_Hd", DBNull.Value);
                    values[12] = new SqlParameter("p_Ma_Dt", DBNull.Value);
                    values[13] = new SqlParameter("p_Han_Tt", DBNull.Value);
                    values[14] = new SqlParameter("p_Ma_Km", DBNull.Value);
                    values[15] = new SqlParameter("p_Ma_Sp", row["Ma_Sp"]);
                    values[0x10] = new SqlParameter("p_Dien_Giai", ps_Dien_Giai);
                    values[0x11] = new SqlParameter("p_Desc_EN", ps_Desc_EN);
                    values[0x12] = new SqlParameter("p_Result", SqlDbType.BigInt);
                    values[0x12].Direction = ParameterDirection.Output;
                    values[0x12].SqlDbType = SqlDbType.Int;
                    try
                    {
                        command.CommandText = "CtK0_Save";
                        command.Parameters.Clear();
                        command.Parameters.AddRange(values);
                        command.ExecuteNonQuery();
                        continue;
                    }
                    catch (Exception exception2)
                    {
                        throw new NotSupportedException("Invalid expression", exception2);
                    }
                }
                SqlParameter[] parameterArray2 = new SqlParameter[] { 
                    new SqlParameter("p_Moi_Sua", true), new SqlParameter("p_Stt", str), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Nh_Ct", "3"), new SqlParameter("p_Ma_Ct", "PKCT"), new SqlParameter("p_Ngay_Ps", pd_Ngay_Ct), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_BookNum", DBNull.Value), new SqlParameter("p_So_Ct", obj2), new SqlParameter("p_Ma_Bp", DBNull.Value), new SqlParameter("p_Ma_Hd", DBNull.Value), new SqlParameter("p_Ma_Dt", DBNull.Value), new SqlParameter("p_Ong_Ba", DBNull.Value), new SqlParameter("p_Dia_Chi", DBNull.Value), new SqlParameter("p_Dien_Giai", ps_Dien_Giai), new SqlParameter("p_Desc_EN", ps_Desc_EN), 
                    new SqlParameter("p_Ma_Tte", ps_Ma_Tte), new SqlParameter("p_Ty_Gia", 1), new SqlParameter("p_Ty_Gia_Ht", 1), new SqlParameter("p_TTien_NtN", num2), new SqlParameter("p_TTien_NtC", num2), new SqlParameter("p_TTien", num2), new SqlParameter("p_TTien_VAT0", DBNull.Value), new SqlParameter("p_IL_TTien", DBNull.Value), new SqlParameter("p_UN_TTien", DBNull.Value), new SqlParameter("p_Ct_Di_Kem", DBNull.Value), new SqlParameter("p_Ma_Loai1", DBNull.Value), new SqlParameter("p_Ma_Loai2", DBNull.Value), new SqlParameter("p_Ma_Loai3", DBNull.Value), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Locked", pb_Locked), 
                    new SqlParameter("p_Result", SqlDbType.BigInt)
                 };
                parameterArray2[0x20].Direction = ParameterDirection.Output;
                parameterArray2[0x20].SqlDbType = SqlDbType.Int;
                try
                {
                    command.CommandText = "CtK_Save";
                    command.Parameters.Clear();
                    command.Parameters.AddRange(parameterArray2);
                    command.ExecuteNonQuery();
                }
                catch (Exception exception3)
                {
                    throw new NotSupportedException("Invalid expression", exception3);
                }
                access.PostSoCai(eZConnection, transaction, str, pi_Posted);
                transaction.Commit();
                AppDataAccess.CloseConnection(eZConnection);
            }
        }

        public bool DeleteDetailRowHS(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SHeSo_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public bool DeleteRowPB(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SChiTiet_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataSet GetDataSet(string ps_Subsidiary, object ps_Loai_PB)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Loai_PB", ps_Loai_PB) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SPhanBo_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataTable GetDetailTableHS(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SHeSo_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public DataTable GetTablePB(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_KeyCode, ref object pi_TTien0, ref object pi_TTien1)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_SPBID", pi_KeyCode), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_TTien0", SqlDbType.BigInt), new SqlParameter("p_TTien1", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Decimal;
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Decimal;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SChiTiet_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                pi_TTien0 = values[4].Value;
                pi_TTien1 = values[5].Value;
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public DataTable PhanBo_GTCT(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_SPBID, ref object pi_TTien0, ref object pi_TTien1)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_SPBID", pi_SPBID), new SqlParameter("p_TTien0", SqlDbType.BigInt), new SqlParameter("p_TTien1", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Decimal;
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Decimal;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_GTCT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                pi_TTien0 = values[4].Value;
                pi_TTien1 = values[5].Value;
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public int PhanBo_HSCT(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_SPBID, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_SPBID", pi_SPBID), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_HSCT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[5].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int PhanBo_TLCT(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_SPBID, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_SPBID", pi_SPBID), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_TLCT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[5].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int PhanBo_TTCT(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_SPBID, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_SPBID", pi_SPBID), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_TTCT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[5].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        private int SaveDetailRowHS(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_He_So", row["He_So"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SHeSo_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[4].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveMasterRowHS(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Loai_PB", row["Loai_PB"]), new SqlParameter("p_Tk_Di", row["Tk_Di"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Tk_Den", row["Tk_Den"]), new SqlParameter("p_Tk_Di01", row["Tk_Di01"]), new SqlParameter("p_Tk_Di02", row["Tk_Di02"]), new SqlParameter("p_Tk_Di03", row["Tk_Di03"]), new SqlParameter("p_Tk_Di04", row["Tk_Di04"]), new SqlParameter("p_Tk_Di05", row["Tk_Di05"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[14].Direction = ParameterDirection.Output;
            values[14].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SPhanBo_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[14].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Loai_PB", row["Loai_PB"]), new SqlParameter("p_Tk_Di", row["Tk_Di"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Tk_Den", row["Tk_Den"]), new SqlParameter("p_Tk_Di01", row["Tk_Di01"]), new SqlParameter("p_Tk_Di02", row["Tk_Di02"]), new SqlParameter("p_Tk_Di03", row["Tk_Di03"]), new SqlParameter("p_Tk_Di04", row["Tk_Di04"]), new SqlParameter("p_Tk_Di05", row["Tk_Di05"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[14].Direction = ParameterDirection.Output;
            values[14].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SPhanBo_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                row["Stt"] = values[1].Value;
                num = (int) values[14].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public bool SaveRowHS(bool pb_NewChecked, DataRow po_MasterDataRow, DataTable po_DetailDataTable)
        {
            bool flag = false;
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                if ((row.RowState != DataRowState.Deleted) && (((decimal) row["He_So"]) != 0M))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                AppDataAccess.Delete("SPhanBo_Delete", po_MasterDataRow["Stt"]);
                return false;
            }
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlTransaction transaction = eZConnection.BeginTransaction();
            flag = true;
            if (this.SaveMasterRowHS(eZConnection, transaction, pb_NewChecked, po_MasterDataRow) == 1)
            {
                foreach (DataRow row2 in po_DetailDataTable.Rows)
                {
                    row2.EndEdit();
                    if ((row2.RowState != DataRowState.Deleted) && (((decimal) row2["He_So"]) != 0M))
                    {
                        if (((row2.RowState != DataRowState.Added) && (row2.RowState != DataRowState.Modified)) || (this.SaveDetailRowHS(eZConnection, transaction, po_MasterDataRow["Stt"], row2) == 1))
                        {
                            continue;
                        }
                        flag = false;
                        break;
                    }
                    if (row2.RowState == DataRowState.Deleted)
                    {
                        this.DeleteDetailRowHS(eZConnection, transaction, row2["Stt0", DataRowVersion.Original]);
                    }
                    else
                    {
                        this.DeleteDetailRowHS(eZConnection, transaction, row2["Stt0"]);
                    }
                }
                if (flag)
                {
                    po_DetailDataTable.AcceptChanges();
                    po_MasterDataRow.Table.AcceptChanges();
                    transaction.Commit();
                    AppDataAccess.CloseConnection(eZConnection);
                    return true;
                }
            }
            transaction.Rollback();
            AppDataAccess.CloseConnection(eZConnection);
            return false;
        }

        public int SaveRowPB(SqlConnection po_Connection, SqlTransaction po_Transaction, string ps_Subsidiary, object po_MasterKey, object pd_Ngay_Ct1, object pd_Ngay_Ct2, DataRow poRow)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", poRow["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp", poRow["Ma_Sp"]), new SqlParameter("p_Tien1", poRow["Tien1"]), new SqlParameter("p_He_So", poRow["He_So"]), new SqlParameter("p_Tien0", poRow["Tien0"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[9].Direction = ParameterDirection.Output;
            values[9].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SChiTiet_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[9].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public bool SaveTablePB(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object po_MasterKey, DataTable po_DetailTable)
        {
            bool flag = true;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlTransaction transaction = eZConnection.BeginTransaction();
            foreach (DataRow row in po_DetailTable.Rows)
            {
                row.EndEdit();
                if ((row.RowState != DataRowState.Deleted) && ((((decimal) row["Tien0"]) != 0M) || (((decimal) row["He_So"]) != 0M)))
                {
                    if (this.SaveRowPB(eZConnection, transaction, ps_Subsidiary, po_MasterKey, pd_Ngay_Ct1, pd_Ngay_Ct2, row) == 1)
                    {
                        continue;
                    }
                    flag = false;
                    break;
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    this.DeleteRowPB(eZConnection, transaction, row["Stt0", DataRowVersion.Original]);
                }
                else
                {
                    this.DeleteRowPB(eZConnection, transaction, row["Stt0"]);
                }
            }
            if (flag)
            {
                po_DetailTable.AcceptChanges();
                transaction.Commit();
                AppDataAccess.CloseConnection(eZConnection);
                return true;
            }
            transaction.Rollback();
            AppDataAccess.CloseConnection(eZConnection);
            return false;
        }
    }
}

